package com.honey.serializable.xml;

import java.util.List;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.Method;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.serializable.BaseSerializableObjectMethod;

public class XmlSerial extends BaseSerializableObjectMethod{

	@Override
	public void appendJsonMethod(AbstractJavaCompilation abstractJavaCompilation) {
		List<Field> fields  =   abstractJavaCompilation.getFields() ;
		Method jsonMethod = new Method("toXml", JDKFullyQualifiedJavaType.getStringInstance());
		StringBuilder sb = new StringBuilder();
		jsonMethod.addBodyLine(" StringBuilder answer = new StringBuilder() ");
		sb.setLength(0);
		sb.append("answer.append(\"<").append(abstractJavaCompilation.getType().getLongName()).append(">\\n\")");
		jsonMethod.addBodyLine(sb.toString());
		for(Field field : fields  ){
			if(field.getDecoration().isFinal() || field.getDecoration().isStatic()){
				continue;
			}
			sb.setLength(0);
			sb.append("answer.append(\"  <").append(field.getName()).append(">\")")
			.append(".append(").append(getValue(field))
			.append(")")
			.append(".append(\"</").append(field.getName()).append(">\\n\")")
			;
			jsonMethod.addBodyLine(sb.toString());
		}
		sb.setLength(0);
		sb.append("answer.append(\"</").append(abstractJavaCompilation.getType().getLongName()).append(">\\n\")");
		jsonMethod.addBodyLine(sb.toString());
		
		jsonMethod.addBodyLine("return answer.toString()");
		abstractJavaCompilation.addMethod(jsonMethod);
	}
}
